(function($){
//关联
$.fn.goodsModal = function(options){
	options = $.extend({
		type : 'goods', //[goods|category]
		text : '',
		api : '',
		target : null
	}, options);
	return this.each(function(){
		var _this = $(this);
		$(options.target).on('click', 'a.del', function(){
			var parent = $(this).parent();
			parent.addClass('remove').heightAnimate(0, function(){parent.remove()});
		});
		var ids = [];
		$(options.target).find(':hidden').each(function(){ids.push($(this).val())});
		_this.click(function(){
			var win = $.window(), modal = _this.data('goodsModal');
			if(!!!modal){
				modal = $('<div class="goods">\
					<div class="title ge-bottom ge-light"><a href="javascript:void(0)">×</a>搜索'+options.text+'</div>\
					<div class="view">\
						<table>\
							<thead>\
								<tr>\
									<th class="name">\
										<font>名称</font>\
										<div><span>搜索</span><input type="text" class="keyword" placeholder="输入'+options.text+'搜索" /></div>\
									</th>\
									<th class="btnView"></th>\
								</tr>\
							</thead>\
						</table>\
						<div class="list">\
							<table><tbody></tbody></table>\
						</div>\
					</div>\
					<div class="bottom ge-top ge-light">\
						<div class="mypage"></div>\
						<a href="javascript:void(0)" class="selected hidden"></a>\
					</div>\
				</div>');
				_this.data('goodsModal', modal);
			};
			if(win.width<=768)modal.css({ width:win.width, height:win.height });
			else modal.css({ width:'', height:'' });
			$.overlay(modal, 0);
			modal.find('.title a').click(function(){$.overlay(false)});
			modal.find('.keyword').onkey({
				callback : function(code){
					if(code==13)setList(modal, $(this).val());
				}
			}).prev().click(function(){
				setList(modal, modal.find('.keyword').val());
			});
			setList(modal, modal.find('.keyword').val());
		});
		function setList(modal, keyword, BRSR){
			if(!BRSR)BRSR = 0;
			BRSR = Number(BRSR);
			$.getJSON(options.api+'&keyword='+keyword+'&BRSR='+BRSR, function(json){
				if(!$.isArray(json.data.rs))return;
				var html = '', items = [];
				if($.isArray(json.data.rs)){
					$.each(json.data.rs, function(){
						var name = [];
						name.push('<a href="index.php?app=service&act=city_edit&province_id='+this.province_id+'&id='+this.id+'" target="_blank">'+this.name+'</a>');
                        html += '<tr>\
							'+(options.type=='goods'?'<td class="pic"><div url="'+this.pic+'"></div></td>':'')+'\
							<td class="name">'+name.join('<em>»</em>')+'</td>\
							'+(options.type=='goods'?'<td class="price red"></td>':'')+'\
							<td class="btnView"><a href="javascript:void(0)" '+($.inArray(this.id, ids)>-1?'class="selected"':'')+'></a></td>\
						</tr>';
                        if($.inArray(this.id, ids)>-1)items.push(this);
                    });
				}
				modal.find('.list tbody').html(html);
				modal.find('.list tbody .pic div').loadbackground();
				var pages = json.data.page;
				html = '当前第'+pages.current_page+'页，共'+pages.num_pages+'页 ';
				if(pages.current_page==1){
					html += '<span class="ezr_nav_na">＜</span> ';
				}else{
					html += '<a href="javascript:void(0)" class="ezr_back" BRSR="'+(BRSR-10)+'">＜</a> ';
				}
				if(pages.current_page==pages.num_pages){
					html += '<span class="ezr_nav_na">＞</span>';
				}else{
					html += '<a href="javascript:void(0)" class="ezr_next" BRSR="'+(BRSR+10)+'">＞</a>';
				};
				modal.find('.bottom .mypage').html(html);
				modal.find('.bottom .mypage a').click(function(){
					setList(modal, keyword, $(this).attr('BRSR'));
				});
				modal.find('.bottom .selected').click(function(){
					var html = '';
					$.each(items, function(){
							html += '<li style="border: 1px solid #eee;box-sizing: content-box;height: 30px;padding-top: 7px;">\
								<a class="del" href="javascript:void(0)">－</a>\
								<a href="index.php?app=service&act=city_edit&province_id='+this.province_id+'&id='+this.id+'" target="_blank">\
									<span>'+this.name+'</span>\
								</a>\
								<input type="hidden" class="goods_id" name="city[]" value="'+this.city_id+'" />\
							</li>';
					});
					$(options.target).html(html);
					$.overlay(false);
				});
				modal.find('.list td.btnView a').each(function(i){
					$(this).data('data', json.data.rs[i]).click(function(){
						var data = $(this).data('data');
						if($(this).hasClass('selected')){
							$(this).removeClass('selected');
							var idsTmp = [], itemsTmp = [];
							$.each(items, function(){
								if(data.id!=this.id){
									idsTmp.push(this.id);
									itemsTmp.push(this);
								}
							});
							ids = idsTmp;
							items = itemsTmp;
						}else{
							$(this).addClass('selected');
							ids.push(data.id);
							items.push(data);
						};
						if(items.length)modal.find('.bottom .selected').removeClass('hidden');
						else modal.find('.bottom .selected').addClass('hidden');
					});
				});
			});
		}
	});
};
})(jQuery);
$(function(){
	if(navigator.userAgent.toLowerCase().indexOf('windows')>-1)$('html').addClass('col-windows');
	$(document.body).data('overlay-no-overload', true);
	$(document.body).data('datepicker.options', {readonly:false});
	if($.request() && !$.request('#')){
		var request = $.request();
		if(request.msg){
			$.overloadSuccess('提交成功');
			location.href += '#showd';
		}
		if(request.wxactive){
			$.overloadSuccess('绑定成功');
			location.href += '#wxactive';
		}
	}
	$('form.form-horizontal').coo({
		noLabel : true,
		css : 'error'
	});
	$('[type="number"]').blur(function(){
		var val = Number($(this).val());
		$(this).val(val.numberFormat(2));
	});
	$('input.premobile').click(function(){
		if(this.checked){
			$('div.premobile').addClass('premobile-active');
		}else{
			$('div.premobile').removeClass('premobile-active');
		}
	});
	$('.premobile-content img').each(function(){
		var _this = $(this);
		if(_this.outerWidth(false)>=300){
			_this.removeAttr('width').removeAttr('height').css({width:'100%', height:''});
		}
	});
});